﻿using MangoMes.Commont.DTOs.ProductDTOs;
using MangoMes.Commont.DTOs;
using MediatR;
using MangoMes.Commont.Infrastructures.Interfaces;
using MangoMes.Commont.DapperHelps;

namespace ManagoMes.Read.API.Applications.ProductionWorkOrders
{
    public class ProductionOrdersListHander : IRequestHandler<ProductionOrdersListCommand, CodePageDTO<ProductDTOs>>
    {
        private readonly IBateRepository<ProductDTOs> produc;

        public ProductionOrdersListHander(IBateRepository<ProductDTOs> produc)
        {
            this.produc = produc;
        }

        public async Task<CodePageDTO<ProductDTOs>> Handle(ProductionOrdersListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                CodePageDTO<ProductDTOs> res = new CodePageDTO<ProductDTOs>() { PageCount = 0, TotalCount = 0 };
                string sql = @"SELECT * FROM clientmanagement a RIGHT JOIN productionworkorders b on a.ClientNo=b.CustomerNo 
                               LEFT JOIN productmanage c on b.ProductsNo=c.ProductManageNode where b.State!=2";
                ProductDTOs productDTOs = new ProductDTOs();
                DapperHelper dapper = new DapperHelper();
                var list = produc.GetAll(sql, productDTOs, dapper.MangoMesWrite).ToList();
                if (!string.IsNullOrEmpty(request.ProductionWorkOrdersNo))
                {
                    list = list.Where(x => x.ProductionWorkOrdersNo.Contains(request.ProductionWorkOrdersNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ProductionWorkOrdersName))
                {
                    list = list.Where(x => x.ProductionWorkOrdersName.Contains(request.ProductionWorkOrdersName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.Source))
                {
                    list = list.Where(x => x.Source == request.Source).ToList();
                }
                if (!string.IsNullOrEmpty(request.ProductsNo))
                {
                    list = list.Where(x => x.ProductManageNode.Contains(request.ProductsNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ProductsName))
                {
                    list = list.Where(x => x.ProductManageName.Contains(request.ProductsName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ClientNo))
                {
                    list = list.Where(x => x.ClientNo.Contains(request.ClientNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ClientName))
                {
                    list = list.Where(x => x.ClientName.Contains(request.ClientName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.TicketsType))
                {
                    list = list.Where(x => x.TicketsType == request.TicketsType).ToList();
                }
                if (request.DemandDate != null)
                {
                    list = list.Where(x => x.DemandDate == request.DemandDate).ToList();
                }
                res.Code = 0;
                res.TotalCount = list.Count;
                res.PageCount = (int)Math.Ceiling(list.Count * 1.0 / request.PageSize);
                res.Data = list.OrderByDescending(x => x.ProductionWorkOrdersNo).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                foreach (var item in res.Data)
                {
                    item.DemandDates = item.DemandDate.ToString("yyyy-MM-dd");
                    item.State= item.State == "0" ? "草稿" : "已确认";
                }
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
